Ext.onReady(function(){ 
var sm = new Ext.grid.CheckboxSelectionModel();
var cm=new Ext.grid.ColumnModel([ 
sm,
new Ext.grid.RowNumberer({header: "编号",width:40}),
{header:'流水号',dataIndex:'serialNo',hidden:true},
{header:'股东类型',dataIndex:'stockholderType',width:100},
{header:'股东代码',dataIndex:'stockholderId',width:100},
{header:'股东名称',dataIndex:'stockholderName',width:100},
{header:'出资方式',dataIndex:'holdType',width:100},
{header:'出资币种',dataIndex:'currency',width:100},
{header:'出资占比(%)',dataIndex:'percent',width:100},
{header:'出资金额(元)',dataIndex:'amount',width:100},
{header:'出资日期',dataIndex:'date',width:100},
]); 
var ds=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'customer/listStockholder.do',
method:'POST'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'serialNo'},
{name:'stockholderType'},
{name:'stockholderId'}, 
{name:'stockholderName'},  
{name:'holdType'},  
{name:'percent'},  
{name:'currency'},  
{name:'amount'},
{name:'date'},
]) 
}); 
ds.load({params:{start:0,limit:10}}); 

var stockholderTypeStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=StockholderType',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
stockholderTypeStore.load();

var holdTypeStore=new Ext.data.Store({ 
	proxy:new Ext.data.HttpProxy({
	url:'showType.do?type=HoldType',
	method:'GET'
	}), 
	reader:new Ext.data.JsonReader({ },[
	{name:'name'}, 
	{name:'code'}
	]) 
	}); 
holdTypeStore.load();

var currencyStore=new Ext.data.Store({ 
proxy:new Ext.data.HttpProxy({
url:'showType.do?type=Currency',
method:'GET'
}), 
reader:new Ext.data.JsonReader({ },[
{name:'name'}, 
{name:'code'}
]) 
}); 
currencyStore.load();

var grid=new Ext.grid.GridPanel({ 
id:'myCustomers',
renderTo:"customer_list", 
stripeRows:true,//斑马线效果
title:"股东列表", 
height:500, 
cm:cm,
store: ds,
loadMask: { msg: "加载中..." }, 
viewConfig: {   
                            forceFit:true   
            },   
            bbar: new Ext.PagingToolbar({   
                pageSize: 10,   
                store: ds, 
                displayInfo: true,   
                displayMsg: '第{0} 到 {1} 条数据 共{2}条',   
                emptyMsg: "没有数据"   
            }),
 tbar: [
 {text:'新增',id: 'btnAdd'},
 '-',
 {text:'查看/编辑',handler:checkMyCustomer},
 '-',
 {text:'删除',handler:deleteCustomer}
 ]              
});

//新增客户表单
addForm = new Ext.form.FormPanel({ 
id: 'addCustomerForm', 
width: 580, 
height: 220, 
//样式 
bodyStyle: 'margin:3px 3px 3px 3px', 
frame: true, 
xtype: 'filedset', 
labelWidth: 100, 
items: 
[ { xtype: 'fieldset', 
title: '股东信息', 
collapsible: true, 
autoHeight: true, 
autoWidth: true, 
items: 
[
 { layout : "form",
    items : [{
    	xtype: 'hidden', 
    	id : "serialNo",
    	fieldLabel: 'ID',
    	allowBlank: true
}]},
{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo',         	
        	   id : "stockholderType",
               store :stockholderTypeStore,
               fieldLabel: '股东类型',
               valueField : 'code',
               displayField : 'name',
               emptyText : '请选择股东类型...',
               allowBlank : false,
               blankText:'请选择股东类型',
               typeAhead : true,
               width: 150,
               mode:'remote',  
               hiddenName:'stockholderType',
               editable: false,
               triggerAction : 'all',
               selectOnFocus : true,
               listeners:{  	        	   
	        	   select : function(combo, record,index){
	    				if(combo.value == null){  
	    				  	Ext.getCmp("stockholderName").setVisible(false);
	    				  	Ext.getCmp("stockholderId").setVisible(false);
	    				  	Ext.getCmp("holdType").setVisible(false);
	    				   	Ext.getCmp("percent").setVisible(false);
	    				   	Ext.getCmp("currency").setVisible(false);
	    				   	Ext.getCmp("amount").setVisible(false);
	    					Ext.getCmp("date").setVisible(false);
	    				}
	    				else{
	    					Ext.getCmp("stockholderName").setVisible(true);
	    				  	Ext.getCmp("stockholderId").setVisible(true);
	    				  	Ext.getCmp("holdType").setVisible(true);
	    				   	Ext.getCmp("percent").setVisible(true);
	    				   	Ext.getCmp("currency").setVisible(true);
	    				   	Ext.getCmp("amount").setVisible(true);
	    					Ext.getCmp("date").setVisible(true);
	    					
	    					if(combo.value == '1'){
	    						label =Ext.getCmp('stockholderId').getEl().parent().parent().first();
	    						label.dom.innerHTML ='组织机构代码:';
	    					}	
	    						
	    					else if(combo.value == '2'){
	    						label =Ext.getCmp('stockholderId').getEl().parent().parent().first();
	    						label.dom.innerHTML ='股东身份证号:';
	    					}

	    				}
	    				
						
					} 
	           }

}]},
     {  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
      
}]} 
]},

{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'textfield',        	
	        	id: 'stockholderName', 
	           	name: 'stockholderName', 
	           	fieldLabel: '股东名称',
	               allowBlank : false,
	           	editable: true,  
	           	width: 150 
	        	

}]},
     {  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
        	   xtype: 'textfield',        	
	        	id: 'stockholderId', 
	           	name: 'stockholderId', 
	           	fieldLabel: '代码',
	               allowBlank : false,
	           	editable: true,  
	           	width: 150 
}]} 
]},


{layout:"column",
items:[{
	columnWidth : .5, // 该列有整行中所占百分比
           layout : "form",
           items : [{
        	   xtype: 'combo', 
        	   id : "holdType",
              store :holdTypeStore,
              fieldLabel: '出资方式',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择出资方式...',
              editable: false,
              allowBlank : false,
              typeAhead : true,
              width: 150,
              mode:'remote',  
              hiddenName:'holdType',
              triggerAction : 'all',
              selectOnFocus : true
}]},
     {  columnWidth : .5, 	      	   
	 layout : "form",
           items : [{
        	   xtype: 'combo', 
        	   id : "currency",
              store :currencyStore,
              fieldLabel: '出资币种',
              valueField : 'code',
              displayField : 'name',
              emptyText : '请选择币种...',
              editable: false,
              allowBlank : false,
              typeAhead : true,
              width: 150,
              mode:'remote',  
              hiddenName:'currency',
              triggerAction : 'all',
              selectOnFocus : true
}]} 
]},
{layout:"column",
	items:[{
		columnWidth : .5, // 该列有整行中所占百分比
	           layout : "form",
	           items : [{
	        	   	xtype: 'textfield', 			
	        	   	id:'percent',
	   				name: 'percent', 
	   				fieldLabel: '出资占比(%)', 
	                allowBlank : false,
	   				regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
	   	 	       	regexText:'只能输入整数或小数！' , 
	   	 	       	width: 150
	}]},
	     {  columnWidth : .5, 	      	   
		 	layout : "form",
	           items : [{
	        	   	xtype: 'textfield', 			
	       	   		id:'amount',
	  				name: 'amount', 
	  				fieldLabel: '出资金额(元)', 
	                allowBlank : false,
	  				regex:/^[0-9]+\.{0,1}[0-9]{0,2}$/,
	  	 	       	regexText:'只能输入整数或小数！' , 
	  	 	       	width: 150
	}]} 
	]},
	{layout:"column",
		items:[{
			columnWidth : .5, // 该列有整行中所占百分比
		           layout : "form",
		           items : [{
		        	   xtype: 'datefield',        	
		        	   id: 'date', 
			           	name: 'date', 
			           	fieldLabel: '出资日期', 
			               allowBlank : false,
			           	editable:false, 
			           	width:150 ,

		           	format : 'Y-m-d'
		}]},
		     {  columnWidth : .5, 	      	   
			 	layout : "form",
		           items : [{
		        	   	
		}]} 
		]},
]}]});

//新增用户窗口 
addWin = new Ext.Window({ 
id: 'addCustomerWin', 
title: '客户基本信息-股东信息', 
width: 600, 
height: 260, 
//背景遮罩 
modal: true, 
//重置大小 
resizable: false, 
//当关闭按钮被点击时执行的动作 
closeAction: 'hide', 
plain: true, 
buttonAlign: 'right', 
items:addForm, 
buttons: 
[  
{ text: '保存', id: 'btnSubmit' } ,
{ text: '关闭', handler: function() { Ext.getCmp('addCustomerWin').hide(); } }

] 
});

var config={
    title:"提示",
    width:170,
    msg:'<div align="center">操作成功！</div>',
    closable:false,
    buttons:{cancel:'取消',no:'继续添加'},
    fn: function(btn){
    	if(btn == 'no'){
    		btnAddClick();
    	}
    }
    //icon:Ext.MessageBox.Question
}

function addRoleFunction() {

	
	var addForm = Ext.getCmp("addCustomerForm");

if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'customer/addStockholder.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       		var msgbox = Ext.MessageBox.show(config); 
							Ext.getCmp('addCustomerWin').hide(); 
							Ext.getCmp('myCustomers').store.reload();
                       //Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存未成功！'); 
 
                        } 
                    });  
                    }
 }
  


//添加按钮单击事件 
function btnAddClick() { 
Ext.getCmp("addCustomerWin").show();
addForm.getForm().getEl().dom.reset(); 
Ext.getCmp("stockholderName").setVisible(false);
Ext.getCmp("stockholderId").setVisible(false);
Ext.getCmp("holdType").setVisible(false);
Ext.getCmp("percent").setVisible(false);
Ext.getCmp("currency").setVisible(false);
Ext.getCmp("amount").setVisible(false);
Ext.getCmp("date").setVisible(false);
Ext.getCmp("stockholderType").setVisible(true);
Ext.getCmp("stockholderName").getEl().dom.readOnly = false;
Ext.getCmp("stockholderId").getEl().dom.readOnly = false;

Ext.getCmp("addCustomerWin").hide(); 

Ext.getCmp("addCustomerWin").buttons[0].handler = addRoleFunction; 
Ext.getCmp("addCustomerWin").show(); 
}; 

//新增按钮 
var addUserBtn = Ext.getCmp('btnAdd'); 
addUserBtn.on('click', btnAddClick); 

//删除地址
function deleteCustomer(){
var grid = Ext.getCmp('myCustomers'); 
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要删除的项！"); 
} 
else{
Ext.MessageBox.confirm('提示', '确实要删除所选的项吗？', deleteConfirm);
}
}

function deleteConfirm(btn){
if (btn == 'yes') {
//alert(grid.getSelectionModel().getSelections()[0].data.serialNo);
 Ext.Ajax.request({
  url:'customer/deleteStockholder.do',
  params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
  method:'POST',
  success:function(){
   Ext.MessageBox.alert("提示","删除成功！");
   Ext.getCmp('myCustomers').store.reload(); 
  },failure:function(){
    Ext.MessageBox.alert("提示", "删除失败！"); 
  }
});
}
}

function checkMyCustomer(){
if (grid.getSelectionModel().getSelections()[0] == undefined) { 
Ext.Msg.alert("提示", "请选中要查看的股东！"); 
}
else{
		var serialNo = grid.getSelectionModel().getSelections()[0].data.serialNo;
	
	Ext.getCmp('addCustomerForm').form.reset(); 
	Ext.getCmp("addCustomerWin").buttons[0].handler = editAddressFunction; 
	
           Ext.Ajax.request({
url:'customer/getStockholder.do',
params : { serialNo: grid.getSelectionModel().getSelections()[0].data.serialNo},
method:'POST',
   success:function(response){

 var addJSON = Ext.util.JSON.decode(response.responseText);

 	Ext.getCmp("addCustomerWin").show();
  	Ext.getCmp("serialNo").setValue(addJSON.serialNo);  
	Ext.getCmp("stockholderType").setValue(addJSON.stockholderType);
	Ext.getCmp("stockholderType").setVisible(false);
  	Ext.getCmp("stockholderName").setValue(addJSON.stockholderName);
  	Ext.getCmp("stockholderName").getEl().dom.readOnly = true;
  	Ext.getCmp("stockholderId").setValue(addJSON.stockholderId);
  	Ext.getCmp("stockholderId").getEl().dom.readOnly = true;
  	Ext.getCmp("holdType").setValue(addJSON.holdType);
   	Ext.getCmp("percent").setValue(addJSON.percent);
   	Ext.getCmp("currency").setValue(addJSON.currency);
   	Ext.getCmp("amount").setValue(addJSON.amount);
	Ext.getCmp("date").setValue(addJSON.date);
	
	
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
}); 
}
}


//编辑地址
function editAddressFunction(){
	
	var addForm = Ext.getCmp("addCustomerForm");
if(addForm.form.isValid()){
	 addForm.form.doAction('submit', { 

                    url : 'customer/editStockholder.do', 
 
                    method : 'post', 
 
                       
                       success : function() { 

                       
                       Ext.getCmp('myCustomers').store.reload();
                       Ext.Msg.alert('提示', '保存成功！'); 
 
                       }, 
                        failure : function(form, action) { 

                           Ext.Msg.alert('提示', '保存失败！'); 
 
                        } 
                    }); 
                    }
}

function rowdblclickFn(grid, rowIndex, e){//双击事件  
             var row = grid.getStore().getAt(rowIndex); 
             var selectedCustId = row.get("custId");
             Ext.Ajax.request({
  url:'checkRequest.do',
  method:'GET',
  success:function(){
  window.location.href="checkMyCustomer.do?CustId="+selectedCustId;
  },failure:function(){
    Ext.MessageBox.alert("请求异常！");
  }
});  
           //  window.location.href="view/customer/CheckCustomer_General.jsp?custId="+selectedCustId;
        }   
        grid.addListener('rowdblclick',checkMyCustomer);
/*grid.on('rowdblclick',function(){ window.location.href="view/customer/CheckCustomer_General.jsp" });*/
}); 